﻿//Copyright 2011 University of Southampton 
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using UniversityOfSouthampton.Taxonomy;
using Microsoft.SharePoint.Utilities;
using SharePointEntityProject.EntityEditorLib;


namespace SharePointEntityProjectLibrary
{

    /// <summary>
    /// All taxonomy code goes here
    /// </summary>
    public class TaxonomyManagementProxyLib:IDisposable
    {

        
        private string SITE_URL;
        private SPSite site;
        private SPWeb web;

        string proxyOperationAssembly = UniversityOfSouthampton.Taxonomy.Properties.Resources.UniversityOfSouthamptonTaxonomySharepointDLLName;
        string createTaxonomyGroupClassName = UniversityOfSouthampton.Taxonomy.Properties.Resources.CreateGroupClassName;
        string createTaxonomySetClassName = UniversityOfSouthampton.Taxonomy.Properties.Resources.CreateTermSetClassName;
        string createFieldClassName = UniversityOfSouthampton.Taxonomy.Properties.Resources.CreateFieldClassName;

        public TaxonomyManagementProxyLib(string siteUrl)
        {
            SITE_URL = siteUrl;
            site = new SPSite(SITE_URL);
            web = site.OpenWeb();

             
        }


        public void Dispose()
        {
            this.web.Dispose();
            this.site.Dispose();
        }



        public TaxonomyCreateFieldResult CreateNewTaxonomyField(FieldInfo fieldInfo, TaxonomyCreateTermSetResult termSet)
        {
            //Create input object
            TaxonomyCreateFieldArgs inputArgs = new TaxonomyCreateFieldArgs()
            {
                Id = termSet.Id.ToString(),
                StoreId = termSet.StoreId,
                Name = fieldInfo.Name,
                Description = fieldInfo.Description,
               // Type = fieldInfo.Type
               AllowMultipleValues = true,
               isRequired = false,
                URL = this.SITE_URL
                
            };

            //Call full trust proxy
            Object obj = SPUtility.ExecuteRegisteredProxyOperation(proxyOperationAssembly,
                                                                      createFieldClassName,
                                                                      inputArgs);
            //deal with results
            TaxonomyCreateFieldResult MetadataGroupInfo = obj as TaxonomyCreateFieldResult;
            return MetadataGroupInfo;


        }


        public TaxonomyCreateGroupResult CreateMetadatGroup(string Name, string Description)
        {
            //Create input object
            TaxonomyCreateGroupArgs inputArgs = new TaxonomyCreateGroupArgs()
            {
                Name = Name,
                Description = Description,
                URL = this.SITE_URL
            }; 

            //Call full trust proxy
            Object obj = SPUtility.ExecuteRegisteredProxyOperation(proxyOperationAssembly,
                                                                      createTaxonomyGroupClassName,
                                                                      inputArgs);
            //deal with results
            TaxonomyCreateGroupResult MetadataGroupInfo = obj as TaxonomyCreateGroupResult;
            return MetadataGroupInfo;

        }

        public TaxonomyCreateTermSetResult CreateMetadatTermSet(TaxonomyCreateTermSetArgs inputArgs)
        {

            

            //Call full trust proxy
            Object obj = SPUtility.ExecuteRegisteredProxyOperation(proxyOperationAssembly,
                                                                      createTaxonomySetClassName,
                                                                      inputArgs);
            //deal with results
            TaxonomyCreateTermSetResult results = obj as TaxonomyCreateTermSetResult;
            return results;
        }



    }
}
